﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using Public;

namespace QLNhanKhau
{
    public partial class UCUpdateChuyenDi : UserControl
    {
        public UCUpdateChuyenDi()
        {
            InitializeComponent();
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            if (txtSoCK.Text == "")
            {
                MessageBox.Show("Nhập số chuyển khẩu cần tìm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtSoCK.Focus();
                if (dgvChuyenKhau.DataSource != null)
                {
                    dgvChuyenKhau.DataSource = null;
                    txtHoTen.ResetText();
                    txtCMND.ResetText();
                    dateNgaySinh.ResetValue();
                    cmbNoiSinh.Refresh();
                    cmbQuanHe.Refresh();
                    cmbQuocTich.Refresh();
                    cmbTonGiao.Refresh();

                    cmbTinh.Refresh();
                    cmbQuanHuyen.Refresh();
                    cmbPhuongXa.Refresh();

                    cmbDanToc.Refresh();
                    txtThuongTru.ResetText();
                    txtSoChuyenKhau.ResetText();
                    txtSoSHK.ResetText();
                    txtLyDoChuyen.ResetText();
                    dateNgayCap.ResetValue();
                    //dateNgayNhan.ResetValue();
                    txtChuHo.ResetText();
                    //txtNguoiCap.ResetText();
                }
                return;

            }
            int SoCK = int.Parse(txtSoCK.Text);
            List<ChuyenKhauPublic> list_ck = ChuyenKhauBLL.LoadChuyenKhauBySoCK(SoCK);
            if (list_ck.Count == 0)
            {
                dgvChuyenKhau.DataSource = null;
                MessageBox.Show("Không tìm thấy!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSoCK.ResetText();
                txtSoCK.Focus();
                groupBox2.Enabled = false;
                
                return;
            }
            else
            {
                groupBox2.Enabled = true;
                dgvChuyenKhau.DataSource = list_ck;
                //dgvChuyenKhau.Columns[0].HeaderText = "Mã chuyển khẩu";
                //dgvChuyenKhau.Columns[1].HeaderText = "Mã nhân khẩu";
                dgvChuyenKhau.Columns[2].HeaderText = "Số";        
                dgvChuyenKhau.Columns[3].HeaderText = "Nơi chuyển";
                dgvChuyenKhau.Columns[4].HeaderText = "Lý do chuyển";
                dgvChuyenKhau.Columns[5].HeaderText = "Ngày cấp";
                //dgvChuyenKhau.Columns[6].HeaderText = "Cán bộ nơi cấp";
                dgvChuyenKhau.Columns[7].HeaderText = "Người cấp";
                //dgvChuyenKhau.Columns[8].HeaderText = "Chức vụ người cấp";
                dgvChuyenKhau.Columns[9].HeaderText = "Nơi cấp";
                //dgvChuyenKhau.Columns[10].HeaderText = "Cán bộ nơi nhận";
                //dgvChuyenKhau.Columns[11].HeaderText = "Ngày nhận";
                dgvChuyenKhau.Columns[12].HeaderText = "Người nhận";
                //dgvChuyenKhau.Columns[13].HeaderText = "Chức vụ người nhận";
                dgvChuyenKhau.Columns[14].HeaderText = "Thường trú";
                dgvChuyenKhau.Columns[15].HeaderText = "Chủ hộ";
                dgvChuyenKhau.Columns[16].HeaderText = "Quan hệ";
                dgvChuyenKhau.Columns[0].Visible = false;
                dgvChuyenKhau.Columns[1].Visible = false;
                //dgvChuyenKhau.Columns[2].Visible = false;
                //dgvChuyenKhau.Columns[3].Visible = false;
                //dgvChuyenKhau.Columns[4].Visible = false;
                //dgvChuyenKhau.Columns[5].Visible = false;
                dgvChuyenKhau.Columns[6].Visible = false;
                dgvChuyenKhau.Columns[7].Visible = false;
                dgvChuyenKhau.Columns[8].Visible = false;
                dgvChuyenKhau.Columns[9].Visible = false;
                dgvChuyenKhau.Columns[10].Visible = false;
                dgvChuyenKhau.Columns[11].Visible = false;
                dgvChuyenKhau.Columns[12].Visible = false;
                dgvChuyenKhau.Columns[13].Visible = false;
                //dgvChuyenKhau.Columns[14].Visible = false;
                dgvChuyenKhau.Columns[15].Visible = false;
                dgvChuyenKhau.Columns[16].Visible = false;
                //dgvChuyenKhau.Columns[16].Visible = false;
                txtNoiCap.Text = ThietLapBLL.LoadDiaChi();
            }
        }
        int _NoiChuyen;
        int _MaCK;
        private void dgvChuyenKhau_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewCell cell = null;

            foreach (DataGridViewCell select in dgvChuyenKhau.SelectedCells)
            {
                cell = select;
            }
            if (cell != null)
            {
                DataGridViewRow row = cell.OwningRow;

                
                _MaCK = (int)row.Cells[0].Value;
                NhanKhauPublic nk = NhanKhauBLL.LoadNhanKhauByMaNK((int)row.Cells[1].Value);
                txtHoTen.Text = nk.HoTen;
                if ((bool)nk.GioiTinh == true)
                    sbtGioiTinh.Value = true;
                else
                    sbtGioiTinh.Value = false;
                dateNgaySinh.Value = (DateTime)nk.NgaySinh;
                cmbNoiSinh.SelectedValue = (int)nk.NoiSinh;
                cmbDanToc.SelectedValue = (byte)nk.MaDT;
                cmbQuocTich.SelectedValue = (byte)nk.MaQT;
                cmbTonGiao.SelectedValue = (byte)nk.MaTG;
                txtTenGoiKhac.Text = nk.TenGoiKhac;
                txtCMND.Text = nk.CMND;
                txtQueQuan.Text = nk.QueQuan;
                txtThuongTru.Text = row.Cells[14].Value.ToString();
                txtSoChuyenKhau.Text = row.Cells[2].Value.ToString();
                txtChuHo.Text = row.Cells[15].Value.ToString();
                txtLyDoChuyen.Text = row.Cells[4].Value.ToString();
                cmbQuanHe.SelectedValue = (byte)row.Cells[16].Value;
                cmbNguoiCap.Text = row.Cells[7].Value.ToString();
                cmbChucVu.SelectedValue = (byte)row.Cells[8].Value;
                dateNgayCap.Value = (DateTime)row.Cells[5].Value;

                if (row.Cells[3].Value.ToString().Length < 7)
                {
                    rdtNgoaiXa.Checked = true;
                    //cmbTinh.Enabled = true;
                    //cmbQuanHuyen.Enabled = true;
                    //cmbPhuongXa.Enabled = true;
                    txtSoSHK.Enabled = false;
                    if (row.Cells[3].Value.ToString().Length == 5)
                    {
                        cmbTinh.SelectedIndex = int.Parse(('0' + row.Cells[3].Value.ToString()).Substring(0, 1));
                        cmbQuanHuyen.SelectedIndex = int.Parse(('0' + row.Cells[3].Value.ToString()).Substring(0, 3));
                        cmbPhuongXa.SelectedIndex = (int)row.Cells[3].Value;
                    }
                    if (row.Cells[3].Value.ToString().Length == 6)
                    {
                        cmbTinh.SelectedIndex = int.Parse((row.Cells[3].Value.ToString()).Substring(2));
                        cmbQuanHuyen.SelectedIndex = int.Parse((row.Cells[3].Value.ToString()).Substring(2, 2));
                    }

                }
                else
                {
                    rdtNoiChuyen.Checked = true;
                    groupBox1.Enabled = false;
                    cmbTinh.Enabled = false;
                    cmbQuanHuyen.Enabled = false;
                    cmbPhuongXa.Enabled = false;
                    txtSoSHK.Text = row.Cells[3].Value.ToString();
                }
            }
        }
        string NguoiNhan;
        byte ChucVuNguoiNhan;
        private void btnSua_Click(object sender, EventArgs e)
        {
            
            if (txtHoTen.Text == "" || txtQueQuan.Text == "" || txtSoChuyenKhau.Text == ""|| txtLyDoChuyen.Text == "" || 
                dateNgayCap.Text == "" || dateNgayCap.Text == "")
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if(txtSoSHK.Text==""&&cmbPhuongXa.SelectedIndex==-1)
            {
                MessageBox.Show("Vui lòng chọn nơi chuyển", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (cmbTinh.Enabled == true)
            {
                _NoiChuyen = (int)cmbPhuongXa.SelectedValue;
                NguoiNhan = null;
                ChucVuNguoiNhan = 0;
            }
            if (txtSoSHK.Enabled == true)
            {
                _NoiChuyen = int.Parse(txtSoSHK.Text);
                NguoiNhan = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCap.SelectedValue).HoTen;
                ChucVuNguoiNhan = (byte)cmbChucVu.SelectedValue;
            }
          //  public static void UpdateChuyenDi(byte MaQH, string ChuHo, int NoiChuyen, string LyDoChuyen, string NguoiCap, int ChucVu, int MaCK)
            string NguoiCap = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCap.SelectedValue).HoTen;
            ChuyenKhauBLL.UpdateChuyenDi((byte)cmbQuanHe.SelectedValue, txtChuHo.Text, _NoiChuyen, txtLyDoChuyen.Text,NguoiCap, (byte)cmbChucVu.SelectedValue,NguoiNhan,ChucVuNguoiNhan, _MaCK);
            MessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            btnTim_Click(sender, e);
        }

        private void txtSoSHK_Leave(object sender, EventArgs e)
        {
            int SoSHK;
            if (txtSoSHK.Text == "")
                SoSHK = 0;
            else
                SoSHK = int.Parse(txtSoSHK.Text);
            SoHoKhauPublic shk= SoHoKhauBLL.LoadSoHoKhauBySoSHK(SoSHK);
            if (shk == null)
            {
                this.balloonTip2.ShowBalloon(txtSoSHK);
                this.balloonTip2.SetBalloonCaption(txtSoSHK, "Thông báo");
                this.balloonTip2.SetBalloonText(txtSoSHK, "Không có sổ sổ hộ khẩu này trong hệ thống");
                txtSoSHK.Focus();
            }
            else
            {
                List<NhanKhauPublic> list_nk = NhanKhauBLL.LoadNhanKhauByMaSHK(shk.MaSHK);
                txtChuHo.Text = list_nk.Find(item => item.MaQH == 1).HoTen;
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult resuft = MessageBox.Show("Xóa khai tử này!", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            switch (resuft)
            {
                case DialogResult.OK:
                    //NhanKhauBLL.UpdateKhaiTu(_MaKT,null);
                    KhaiTuBLL.DeleteKhaiTu(_MaCK);
                    UCUpdateChuyenDi_Load(sender, e);
                    break;
                case DialogResult.Cancel:
                    break;
            }
        }

        private void UCUpdateChuyenDi_Load(object sender, EventArgs e)
        {
            cmbTonGiao.DataSource = TonGiaoBLL.LoadTonGiao();
            cmbTonGiao.DisplayMember = "TenTonGiao";
            cmbTonGiao.ValueMember = "MaTG";

            cmbDanToc.DataSource = DanTocBLL.LoadDanToc();
            cmbDanToc.DisplayMember = "TenDanToc";
            cmbDanToc.ValueMember = "MaDT";
            //load quoc tich
            cmbQuocTich.DataSource = QuocTichBLL.LoadQuocTich();
            cmbQuocTich.DisplayMember = "TenQuocTich";
            cmbQuocTich.ValueMember = "MaQT";
            
            cmbQuanHe.DataSource = QuanHeBLL.LoadQuanHe();
            cmbQuanHe.DisplayMember = "TenQuanHe";
            cmbQuanHe.ValueMember = "MaQH";

            cmbNguoiCap.DataSource = NguoiDungBLL.LoadNguoiCap();
            cmbNguoiCap.DisplayMember = "HoTen";
            cmbNguoiCap.ValueMember = "MaND";

            cmbChucVu.DataSource = ChucVuBLL.LoadChucVu();
            cmbChucVu.DisplayMember = "TenChucVu";
            cmbChucVu.ValueMember = "MaCV";

            //load cho phần khai sinh

            cmbNguoiCap.DataSource = NguoiDungBLL.LoadChuTich();
            cmbNguoiCap.DisplayMember = "HoTen";
            cmbNguoiCap.ValueMember = "MaND";

            //to màu
            cmbNoiSinh.DataSource = TinhBLL.LoadTinh();
            cmbNoiSinh.DisplayMember = "TenTinh";
            cmbNoiSinh.ValueMember = "MaT";

            cmbTinh.DataSource = TinhBLL.LoadTinh();
            cmbTinh.DisplayMember = "TenTinh";
            cmbTinh.ValueMember = "MaT";
            //cmbTinh.SelectedIndex = -1;

            //cmbQuanHuyen.DataSource = QuanHuyenBLL.LoadQuanHuyenByMaT((int)cmbTinh.SelectedValue);
            //cmbQuanHuyen.DisplayMember = "TenQuanHuyen";
            //cmbQuanHuyen.ValueMember = "MaQH";
            //cmbQuanHuyen.SelectedIndex = -1;

            //cmbPhuongXa.DataSource = PhuongXaBLL.LoadPhuongXaByMaQH((int)cmbQuanHuyen.SelectedValue);
            //cmbPhuongXa.DisplayMember = "TenPhuongXa";
            //cmbPhuongXa.ValueMember = "MaPX";
            //cmbPhuongXa.SelectedIndex = -1;
        }

        private void txtSoCK_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtSoCK.Text.Length > 7 && !Char.IsControl(e.KeyChar)||e.KeyChar==13)
            {
                e.Handled = true;
            }
            if (e.KeyChar == 13)
                btnTim_Click(sender, e);
        }

        private void txtSoChuyenKhau_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void txtLyDoChuyen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtLyDoChuyen.Text.Length > 49 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtChuHo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtChuHo.Text.Length > 29 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void cmbTinh_SelectionChangeCommitted(object sender, EventArgs e)
        {
            cmbQuanHuyen.DataSource = QuanHuyenBLL.LoadQuanHuyenByMaT((int)cmbTinh.SelectedValue);
            cmbQuanHuyen.DisplayMember = "TenQuanHuyen";
            cmbQuanHuyen.ValueMember = "MaQH";
            cmbQuanHuyen.SelectedIndex = -1;
        }

        private void cmbQuanHuyen_SelectionChangeCommitted(object sender, EventArgs e)
        {
            cmbPhuongXa.DataSource = PhuongXaBLL.LoadPhuongXaByMaQH((int)cmbQuanHuyen.SelectedValue);
            cmbPhuongXa.DisplayMember = "TenPhuongXa";
            cmbPhuongXa.ValueMember = "MaPX";
            cmbPhuongXa.SelectedIndex = -1;
        }

        private void rdtNoiChuyen_CheckedChanged(object sender, EventArgs e)
        {
            if (rdtNgoaiXa.Checked == true)
            {
                txtSoSHK.ResetText();
                txtSoSHK.Enabled = false;
                cmbPhuongXa.Enabled = true;
                cmbTinh.Enabled = true;
                cmbQuanHuyen.Enabled = true;
                txtChuHo.Enabled = true;
                txtChuHo.ResetText();
            }
            else
            {
                cmbPhuongXa.Enabled = false;
                cmbTinh.Enabled = false;
                cmbQuanHuyen.Enabled = false;
                txtSoSHK.Enabled = true;
                txtChuHo.Enabled = false;
            }
        }

        private void cmbTinh_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbQuanHuyen.DataSource = QuanHuyenBLL.LoadQuanHuyenByMaT((int)cmbTinh.SelectedIndex);
            cmbQuanHuyen.DisplayMember = "TenQuanHuyen";
            cmbQuanHuyen.ValueMember = "MaQH";
            cmbQuanHuyen.SelectedIndex = -1;
        }
    }
}
